package com.soc.erp.appNews;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import org.nature.framework.annotation.Ask;
import org.nature.framework.annotation.Ctrl;
import org.nature.framework.annotation.Inject;
import org.nature.framework.annotation.Redirect;
import org.nature.framework.bean.JsonData;
import org.nature.framework.core.NatureCtrl;
import org.nature.framework.core.NatureMap;
import org.nature.framework.util.FileUtil;
import org.nature.framework.util.StringUtil;

import com.soc.erp.index.IService;

@Ctrl(namespace = "/appNews")
public class AppNewsCtrl extends NatureCtrl {
	private static final long serialVersionUID = 1L;
	public AppNews appNews = new AppNews();
	public String ask;
	
	public File pic1,pic2,pic3;
	public String startDate = new String();
	public String endDate = new String();
	@Inject
	IService iService;
	
	//public int page_num;
	public NatureMap v = new NatureMap();
	
	public String message;

	@Ask
	public Object list() {
		String sql = "select * from tb_app_news  where deleted = 0";

		if (StringUtil.isNotBank(appNews.getString(AppNews.title))) {
			sql += " and title like '%" + appNews.getString(AppNews.title) + "%'";
		}
		
		if (StringUtil.isNotBank(startDate)) {
			sql += " and date > '" + startDate + "'";
		}
		
		if (StringUtil.isNotBank(endDate)) {
			sql += " and date < '" + endDate + "'";
		}
		
		sql += " order by date desc";
		
		
		//设置回传的页码
		/*if (page_num!=0)
			page.setPageNum(page_num);
		System.out.println("page_num===="+page_num);*/
		//System.out.println(sql);
		page = iService.page(page, sql);
		
		iService.jsPage(page, "jsPage");
		
		List<NatureMap> news_type = iService.list("SELECT c_key,c_value FROM tb_system_config where  item = 'news_type' order by c_key");
		page.put("news_type", news_type);
		
		//List<NatureMap> role = iService.list("select * from tb_role");
		//page.put("role", role);

		return "list.ftl";
	}
	
	@Ask
	public Object insert() {
		String all_name = ""; 
		//String path = getRealPath()+File.separatorChar+"LicensePics"+File.separatorChar+license_pic.getName();
		
		/*if (pic!=null) {
			String path = getRealPath()+File.separatorChar+"NewsPic"+File.separatorChar+pic.getName();
			//System.out.println("path="+path); 
			File file = new File(path);
			file.getParentFile().mkdirs();
			appNews.put(AppNews.picture, "NewsPic"+File.separatorChar+pic.getName());
			FileUtil.move(pic, file);
		}*/
		if (pic1!=null) {
			String path = getRealPath()+File.separatorChar+"NewsPic"+File.separatorChar+pic1.getName();
			File file = new File(path);
			file.getParentFile().mkdirs();
			//appNews.put(AppNews.picture, "NewsPic"+File.separatorChar+pic1.getName());
			FileUtil.move(pic1, file);
			all_name = "NewsPic"+File.separatorChar+pic1.getName();
		}
		
		if (pic2!=null) {
			String path = getRealPath()+File.separatorChar+"NewsPic"+File.separatorChar+pic2.getName();
			File file = new File(path);
			file.getParentFile().mkdirs();
			FileUtil.move(pic2, file);
			all_name = all_name+","+"NewsPic"+File.separatorChar+pic2.getName();
		}
		
		if (pic3!=null) {
			String path = getRealPath()+File.separatorChar+"NewsPic"+File.separatorChar+pic3.getName();
			File file = new File(path);
			file.getParentFile().mkdirs();
			FileUtil.move(pic3, file);
			all_name = all_name+","+"NewsPic"+File.separatorChar+pic3.getName();
		}
		
		if("".equals(all_name))
		{
			
		}
		else{
			appNews.put(AppNews.picture, all_name);
		}
		
		/*if (StringUtil.isNotBank(appNews.getString(AppNews.hotspot))) {
			appNews.put(AppNews.hotspot, "1");
		}
		else
			appNews.put(AppNews.hotspot, "0");*/
		
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		appNews.put(AppNews.date, df.format(new Date()));
		
		appNews.put(AppNews.deleted, 0);
        
	    iService.insert(appNews);
	    return "list"; 
	}
	
	
	//修改
	@Ask
	public Object update(){
		 
		String all_name="";
		String str = "";
		List<String> idList = new ArrayList<String>(3);
		
		String[] aa;
		//List<String> idList1 = new ArrayList<String>();
		if (StringUtil.isNotBank(appNews.getString(AppNews.picture))) 
		{
			//不是第一次修改
			str =  appNews.getString(AppNews.picture);
			//idList1 = Arrays.asList(str.split(","));//根据逗号分隔转化为list 不能用asList 会把list长度固定死
			//idList.addAll(idList1);
			aa = str.split(",");
			for (int i = 0 ; i <aa.length ; i++ ) {

				idList.add(aa[i]);

			} 
			//System.out.println(idList.size());
		}
		else{
			 
		}
		
		if (pic1!=null) {
			String path = getRealPath()+"NewsPic"+File.separatorChar+pic1.getName();
			File file = new File(path);
			file.getParentFile().mkdirs();
			FileUtil.move(pic1, file);
			if (StringUtil.isNotBank(appNews.getString(AppNews.picture))) 
			{
				//暂时不删除旧图 只作替换
				
				if(idList.size()<1)
					idList.add(0, "NewsPic"+File.separatorChar+pic1.getName());
				else
					idList.set(0,"NewsPic"+File.separatorChar+pic1.getName());//修改对应数组中的值		 
			}else{
				all_name = "NewsPic"+File.separatorChar+pic1.getName();	
			}
		}
		
		if (pic2!=null) {
			String path = getRealPath()+"NewsPic"+File.separatorChar+pic2.getName();
			File file = new File(path);
			file.getParentFile().mkdirs();
			FileUtil.move(pic2, file);
			if (StringUtil.isNotBank(appNews.getString(AppNews.picture))) 
			{
				//暂时不删除旧图 只作替换
				if(idList.size()<2)
					idList.add(1, "NewsPic"+File.separatorChar+pic2.getName());
				else
					idList.set(1,"NewsPic"+File.separatorChar+pic2.getName());//修改对应数组中的值
				 
			}else{
				all_name =all_name + "NewsPic"+File.separatorChar+pic2.getName();	
			}
		}
		
		if (pic3!=null) {
			String path = getRealPath()+"NewsPic"+File.separatorChar+pic3.getName();
			File file = new File(path);
			file.getParentFile().mkdirs();
			FileUtil.move(pic3, file);
			if (StringUtil.isNotBank(appNews.getString(AppNews.picture))) 
			{
				if(idList.size()<3)
					idList.add("NewsPic"+File.separatorChar+pic3.getName());
				else
					idList.set(2,"NewsPic"+File.separatorChar+pic3.getName());//修改对应数组中的值
			}else{
				all_name =all_name + "NewsPic"+File.separatorChar+pic3.getName();	
			}
		}
		
		if (StringUtil.isNotBank(appNews.getString(AppNews.picture))) //不是
		{
			all_name = String.join(",", idList);
		}
		else{
		 
		}
	 
		appNews.put(AppNews.picture,all_name);
	 
		
		if (StringUtil.isNotBank(appNews.getString(AppNews.hotspot))) {
			appNews.put(AppNews.hotspot, "1");
		}
		else
			appNews.put(AppNews.hotspot, "0");
		 
		 
		iService.update(appNews);
		
		//不返回list
		iService.byId(appNews);
		List<NatureMap> news_type = iService.list("SELECT c_key,c_value FROM tb_system_config where  item = 'news_type' order by c_key");
		page.put("news_type", news_type);
		
		message = "update";
		return "oper.ftl";
		//return "/WEB-INF/view/appNews/list2.ftl";
		//return new JsonData(0);
	}
		
	@Ask
	public Object toOper() {
		/*List<NatureMap> role = iService.list("select * from tb_role");
		page.put("role", role);*/

		if (StringUtil.isNotBank(appNews.getString(AppNews.id))) {
			iService.byId(appNews);
			ask = "update";
		} else {
			
			ask = "insert";
		}
		
		List<NatureMap> news_type = iService.list("SELECT c_key,c_value FROM tb_system_config where  item = 'news_type' order by c_key");
		page.put("news_type", news_type);
		
		return "oper.ftl";
	}
	
	@Ask
	@Redirect
	public Object delete() {
		appNews = iService.byId(appNews);
		appNews.put("deleted", 1);
		iService.update(appNews);
		return "list";
	}


}
